<template>
  <el-option
    v-for="item in options"
    :key="item.value"
    :label="item.label"
    :value="item.value"
  />
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";

import { getOrganization } from "@/api/organizationManage/index.ts";
import { useStore } from "vuex";

const props = defineProps(["isSearch"]);
const options: IObj[] = reactive([]);
const store = useStore();

async function initOptions() {
  const mapObj: IObj = {};

  if (props.isSearch) {
    options.push({
      label: "全部",
      value: "",
    });
  }

  if (store.getters.organizationMap === undefined) {
    const { data: res } = await getOrganization();

    debugger;
    
    res.data.forEach((item: IObj) => {
      mapObj[item._id] = item.name;

      options.push({
        label: item.name,
        value: item._id,
      });
    });

    store.dispatch("initOrganizationMap", mapObj);
  } else {
    const organizationMap = store.getters.organizationMap;

    for (const item in organizationMap) {
      options.push({
        label: organizationMap[item],
        value: item,
      });
    }
  }
}

initOptions();
</script>
